﻿Public Class Main
    Public Shared Function Main(ByVal args() As String) As Integer
        Try
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
            AddHandler Application.ThreadException, AddressOf UnhandledExceptionHandler_Thread
            AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler_Domain

            Application.EnableVisualStyles()

            ReadCommandLine(args)

            '' Initialize Datastore
            If Not DataStore.Setup.IsUpToDate Then
                If Not SetupDatastore.Manager.SetupAndUpgradeDataStore(Nothing) Then
                    Return -1
                End If
            End If

            Dim clsMainView As New FMain
            Utils.Threading.MainGuiForm = clsMainView
            clsMainView.Show()
            clsMainView.Enabled = False
            Application.DoEvents()

            '' Initialize NetNode
            If NetNode.Settings.SetupLevel_Saved <> NetNode.Settings.SetupLevel_Required Then
                If Not SetupNetNode.Manager.SetupNetNode(clsMainView) Then
                    Return -1
                End If
            End If
            If NetNode.Settings.RSAProvider Is Nothing Then NetNode.Settings.SetupRSAProvider()
            If NetNode.Settings.UniqueId Is Nothing Then NetNode.Settings.SetupUniqueId()
            NetNode.Manager.Start()

            '' Initialize Shared Files
            FileShare.Manager.Start()

            clsMainView.Enabled = True
            Application.Run(clsMainView)
            Return 0
        Catch ex As Exception
            Utils.Misc.HandleException(ex)
            Return -1
        Finally
            FileShare.Manager.Stop()
            NetNode.Manager.Stop()
        End Try
    End Function

    Private Shared Sub UnhandledExceptionHandler_Thread(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
        Try
            MessageBox.Show(e.Exception.Message & vbNewLine & vbNewLine & e.Exception.StackTrace, e.Exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
    Private Shared Sub UnhandledExceptionHandler_Domain(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs)
        Try
            Dim clsEx As Exception = CType(e.ExceptionObject, Exception)
            MessageBox.Show(clsEx.Message & vbNewLine & vbNewLine & clsEx.StackTrace, clsEx.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Shared Sub ReadCommandLine(ByVal args() As String)
        Dim strDBPath As String = Nothing
        Dim strDBPwd As String = Nothing

        For Each strArg As String In args
            Dim iPos As Integer = strArg.IndexOf("="c)
            If iPos > 0 Then
                Dim strKey As String = strArg.Substring(0, iPos).ToUpper
                Dim strVal As String = strArg.Substring(iPos + 1)
                Select Case strKey
                    Case "DBPATH", "DBFILE"
                        strDBPath = strVal
                    Case "DBPASSWORD", "DBPWD"
                        strDBPwd = strVal
                End Select
            End If
        Next

        If Not String.IsNullOrEmpty(strDBPath) AndAlso Not String.IsNullOrEmpty(strDBPwd) Then
            DataStore.Environment.SetActiveDatabase(strDBPath, strDBPwd)
        End If
    End Sub
End Class


